package com.briup.prs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.KeyValueLineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

/**
 * @author adam
 * @date 2022/2/10
 * 对推荐数据清洗 对于已经购买的数据则不进行推荐
 */

public class Step8 extends Configured implements Tool {
    public static class S8Mapper1 extends Mapper<LongWritable, Text, Text, Text> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //用户,商品,行为,时间 切割原始数据
            String[] datas = value.toString().split(",");
            if (datas[2].toLowerCase().equals("paysuccess")) {
                context.write(new Text(datas[0] + ":" + datas[1]), new Text("pay"));
            }
        }
    }
    public static class S8Mapper2 extends Mapper<Text, Text, Text, Text> {
        @Override
        protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
            context.write(key, value);
        }
    }

    public static class S8Reducer extends Reducer<Text, Text, Text, Text> {

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            Text oValue = null;
            for (Text value : values) {
                if (value.toString().contains("pay")) {
                    return;
                } else {
                   oValue= new Text(value.toString());
                }
            }
            context.write(key, oValue);
        }
    }

    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();
//        in1必须是第七步的结果
        String in1 = conf.get("in1");
//        in2 是原始数据
        String in2 = conf.get("in2");
        String out = conf.get("out");
        conf.set(KeyValueLineRecordReader.KEY_VALUE_SEPARATOR,"\t");
        Job job = Job.getInstance(conf, "step8");
        job.setJarByClass(Step8.class);
        MultipleInputs.addInputPath(job,new Path(in1), KeyValueTextInputFormat.class,S8Mapper2.class);
        MultipleInputs.addInputPath(job, new Path(in2),TextInputFormat.class,S8Mapper1.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(S8Reducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job,new Path(out));
        job.waitForCompletion(true);
        return 0;
    }

    public static void main(String[] args) throws Exception {
        ToolRunner.run(new Step8(),args);
    }
}
